package com.gopay.bis.web.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gopay.common.Result;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.wechat.micro.req.MicroPayReqMsg;
import com.gopay.common.domain.bank.wechat.micro.req.MicroRefundQueryReqMsg;
import com.gopay.common.domain.bank.wechat.micro.req.MicroRefundReqMsg;
import com.gopay.common.domain.bank.wechat.micro.req.MicroReverseReqMsg;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.ResponseUtils;
import com.gopay.remote.bank.MicroPayInteractRemoteService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

//@Controller
//@RequestMapping(value = "/micro")
public class MicroPayTest {
    private static Logger logger = LoggerFactory.getLogger(MicroPayTest.class);
    
    @Resource(name = "WECHAT-MICRO-CITIC")
    private MicroPayInteractRemoteService microPay;
    
    @RequestMapping(value = "/pay", method = { RequestMethod.GET })
    public void pay(HttpServletRequest request, HttpServletResponse response) {        
        try {
            String operater = request.getParameter("operator");
            logger.error("=============operater:" + operater + "===================");
            MicroPayReqMsg message = new MicroPayReqMsg();
//            message.setMerID("7551000001");
//            message.setPrivateKeyPath("9d101c97133837e13dde2d32a5054abb");
            message.setMerID("100550019451");
            message.setPrivateKeyPath("8f26cc965e0bbbfcd6c1b30ef6efc162");
            message.setPayUrl("https://pay.swiftpass.cn/pay/gateway");
            message.setBankOrderId(request.getParameter("orderId"));
            message.setAmount(new BigDecimal(request.getParameter("amount")));
            message.setGoodsBody("测试");
            message.setMchCreateIp("192.168.1.233");
            message.setAuthCode(request.getParameter("authCode"));
            message.setOutStlmCd("CITIC");
            message.setBankBusProd(BisBankBusinessProd._25);
            Result result = (Result) microPay.pay(message);
            logger.error("********micro支付返回结果:" + ToStringBuilder.reflectionToString(result));
            String retRes =toXml(result);
            ResponseUtils.renderText(response, retRes);
        } catch (GopayException e) {            
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value = "/query", method = { RequestMethod.GET })
    public void query(HttpServletRequest request, HttpServletResponse response) {       
        try {
            String operater = request.getParameter("operator");
            logger.error("=============operater:" + operater + "===================");
            MicroPayReqMsg message = new MicroPayReqMsg();
//            message.setMerID("7551000001");
//            message.setPrivateKeyPath("9d101c97133837e13dde2d32a5054abb");
            message.setMerID("100550019451");
            message.setPrivateKeyPath("8f26cc965e0bbbfcd6c1b30ef6efc162");
            message.setQueryUrl("https://pay.swiftpass.cn/pay/gateway");
            message.setBankOrderId(request.getParameter("orderId"));
            message.setAmount(new BigDecimal(request.getParameter("amount")));
            message.setGoodsBody("测试");
            message.setMchCreateIp("192.168.1.233");
            message.setAuthCode(request.getParameter("authCode"));
            message.setBank("CITIC");
            Result result = (Result) microPay.orderQuery(message);
            logger.error("********micro订单查询返回结果:" + ToStringBuilder.reflectionToString(result));
            String retRes =toXml(result);
            ResponseUtils.renderText(response, retRes);
        } catch (GopayException e) {
            
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value = "/refund", method = { RequestMethod.GET })
    public void refund(HttpServletRequest request, HttpServletResponse response) {       
        try {
            String operater = request.getParameter("operator");
            logger.error("=============operater:" + operater + "===================");
            MicroRefundReqMsg message = new MicroRefundReqMsg();
//            message.setMerID("7551000001");
//            message.setPrivateKeyPath("9d101c97133837e13dde2d32a5054abb");
            message.setMerID("100550019451");
            message.setPrivateKeyPath("8f26cc965e0bbbfcd6c1b30ef6efc162");
            message.setPayUrl("https://pay.swiftpass.cn/pay/gateway");
            message.setTkOriBankOrderId(request.getParameter("orderId"));
            message.setBankOrderId("TK"+request.getParameter("orderId"));
            message.setAmount(new BigDecimal(request.getParameter("amount")));
            message.setMchCreateIp("192.168.1.233");
            message.setBank("CITIC");
            Result result = (Result) microPay.refund(message);
            logger.error("********micro退款返回结果:" + ToStringBuilder.reflectionToString(result));
            String retRes =toXml(result);
            ResponseUtils.renderText(response, retRes);
        } catch (GopayException e) {
            
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value = "/reverse", method = { RequestMethod.GET })
    public void reverse(HttpServletRequest request, HttpServletResponse response) {       
        try {
            String operater = request.getParameter("operator");
            logger.error("=============operater:" + operater + "===================");
            MicroReverseReqMsg message = new MicroReverseReqMsg();
//            message.setMerID("7551000001");
//            message.setPrivateKeyPath("9d101c97133837e13dde2d32a5054abb");
            message.setMerID("100550019451");
            message.setPrivateKeyPath("8f26cc965e0bbbfcd6c1b30ef6efc162");
            message.setPayUrl("https://pay.swiftpass.cn/pay/gateway");
            message.setCzOriBankOrderId(request.getParameter("orderId"));
            message.setBank("CITIC");
            Result result = (Result) microPay.reverse(message);
            logger.error("********micro冲正返回结果:" + ToStringBuilder.reflectionToString(result));
            String retRes =toXml(result);
            ResponseUtils.renderText(response, retRes);
        } catch (GopayException e) {
            
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value = "/refundQuery", method = { RequestMethod.GET })
    public void refundQuery(HttpServletRequest request, HttpServletResponse response) {       
        try {
            String operater = request.getParameter("operator");
            logger.error("=============operater:" + operater + "===================");
            MicroRefundQueryReqMsg message = new MicroRefundQueryReqMsg();
//            message.setMerID("7551000001");
//            message.setPrivateKeyPath("9d101c97133837e13dde2d32a5054abb");
            message.setMerID("100550019451");
            message.setPrivateKeyPath("8f26cc965e0bbbfcd6c1b30ef6efc162");
            message.setPayUrl("https://pay.swiftpass.cn/pay/gateway");
            message.setTkOriBankOrderId(request.getParameter("orderId"));
            message.setOrderId("TK"+request.getParameter("orderId"));
            message.setAmount(new BigDecimal(request.getParameter("amount")));
            message.setBank("CITIC");
            Result result = (Result) microPay.refundQuery(message);
            logger.error("********micro退款查询返回结果:" + ToStringBuilder.reflectionToString(result));
            String retRes =toXml(result);
            ResponseUtils.renderText(response, retRes);
        } catch (GopayException e) {
            
            e.printStackTrace();
        }
    }
    
    
    public String toXml(Object obj) {
        // XStream xstream=new XStream();
        // XStream xstream=new XStream(new DomDriver()); //直接用jaxp dom来解释
        // 指定编码解析器,直接用jaxp dom来解释
        XStream xstream = new XStream(new DomDriver("GBK"));

        xstream.processAnnotations(obj.getClass());
        return xstream.toXML(obj);
    }

}
